package com.nova.edc.service.system.impl;

import com.nova.edc.dao.system.*;
import com.nova.edc.model.system.*;
import com.nova.edc.model.util.ExcelArea;
import com.nova.edc.service.system.*;
import com.nova.frame.dao.buzz.query.Order;
import com.nova.frame.dao.buzz.query.Query;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.frame.transaction.annotation.Transactional;
import com.nova.frame.utils.JdbcUtils;
import com.nova.module.filter.query.Filter;
import com.nova.module.sql.SqlBuffer;
import com.nova.module.util.StringUtils;
import com.seio.edc.util.IntegerUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


public class PointLightServiceImpl implements PointLightService {

    @Autowired
    private PointLightDao pointLightDao;

    @Autowired
    private DeviceLightDao novaDeviceLightDao;

    @Autowired
    private AreaPointService areaPointService;

    @Autowired
    private DeviceLightService deviceLightService;

    @Autowired
    private CustomerAreaService customerAreaService;

    @Override
    public PointLight insert(PointLight pointLight) {
        pointLight.setTralightid(novaDeviceLightDao.getLightByCode(
                pointLight.getTralightcode(),
                ModelConstant.TRA_LIGHTTYPE).getId());
        pointLight.setLedlightid(novaDeviceLightDao.getLightByCode(
                pointLight.getLedlightcode(),
                ModelConstant.LED_LIGHTTYPE).getId());
        areaPointService.updatePower(ModelConstant.TRA_LIGHTTYPE, pointLight.getPointid(), pointLight.getTranum(), pointLight.getTralightid());
        areaPointService.updatePower(ModelConstant.LED_LIGHTTYPE, pointLight.getPointid(), pointLight.getLednum(), pointLight.getLedlightid());
        if (IntegerUtils.isEmptyAndNull(pointLight.getReallighttype()) && ModelConstant.TRA_LIGHTTYPE == pointLight.getReallighttype()) {
            areaPointService.updatePower(ModelConstant.REAL_LIGHTTYPE, pointLight.getPointid(), pointLight.getTranum(), pointLight.getTralightid());
        } else {
            areaPointService.updatePower(ModelConstant.REAL_LIGHTTYPE, pointLight.getPointid(), pointLight.getLednum(), pointLight.getLedlightid());
        }
        return pointLightDao.insert(pointLight);
    }

    @Override
    @Transactional
    public int insertByExcel(List<ExcelArea> excelAreaList) {
        customerAreaService.insertByExcel(excelAreaList);
        areaPointService.insertByExcel(excelAreaList);
        deviceLightService.insertByExcel(excelAreaList);
        for (ExcelArea excelArea : excelAreaList) {
            if(StringUtils.isNotNullOrEmpty(excelArea.getDevicetypename())
                    && ModelConstant.DEVICE_LIGHTENERGY.equals(excelArea.getDevicetypename())){
                if (ifCanAdd(excelArea)) {
                PointLight pointLight = new PointLight();
                pointLight.setLedlightid(excelArea.getLedlightid());
                pointLight.setLedlightcode(excelArea.getLedlightcode());
                pointLight.setLednum(excelArea.getLednum());
                pointLight.setTralightid(excelArea.getTralightid());
                pointLight.setTralightcode(excelArea.getTralightcode());
                pointLight.setTranum(excelArea.getTranum());
                pointLight.setPointid(excelArea.getPointid());
                pointLight.setAreaid(excelArea.getAreaid());
                pointLight.setCustomerinfoid(excelArea.getCustomerinfoid());
                pointLight.setReallighttype(excelArea.getReallighttype());
                pointLightDao.insert(pointLight);
                //修改节点总功率
                areaPointService.updatePower(ModelConstant.TRA_LIGHTTYPE, excelArea.getPointid(), excelArea.getTranum(), excelArea.getTralightid());
                areaPointService.updatePower(ModelConstant.LED_LIGHTTYPE, excelArea.getPointid(), excelArea.getLednum(), excelArea.getLedlightid());
                if (!IntegerUtils.isEmptyAndNull(excelArea.getReallighttype()) && ModelConstant.TRA_LIGHTTYPE == excelArea.getReallighttype()) {
                    areaPointService.updatePower(ModelConstant.REAL_LIGHTTYPE, excelArea.getPointid(), excelArea.getTranum(), excelArea.getTralightid());
                } else {
                    areaPointService.updatePower(ModelConstant.REAL_LIGHTTYPE, excelArea.getPointid(), excelArea.getLednum(), excelArea.getLedlightid());
                }
            }
            }
        }
        return excelAreaList.size();
    }

    public boolean ifCanAdd(ExcelArea excelArea) {
        return excelArea.getReallighttype() != null
                && StringUtils.isNotNullOrEmpty(excelArea.getTralightcode())
                && StringUtils.isNotNullOrEmpty(excelArea.getLedlightcode())
                && IntegerUtils.isEmptyAndNull(excelArea.getTranum())
                && IntegerUtils.isEmptyAndNull(excelArea.getLednum());
    }

    @Override
    public int delete(PointLight pointLight) {
        return pointLightDao.deleteByExample(pointLight);
    }

    @Override
    public int deleteById(Integer id) {
            /*更改节点功率*/
        PointLight pointLight = pointLightDao.getById(String.valueOf(id));
        areaPointService.updatePower(ModelConstant.TRA_LIGHTTYPE, pointLight.getPointid(), 0 - pointLight.getTranum(), pointLight.getTralightid());
        areaPointService.updatePower(ModelConstant.LED_LIGHTTYPE, pointLight.getPointid(), 0 - pointLight.getLednum(), pointLight.getLedlightid());
        if (IntegerUtils.isEmptyAndNull(pointLight.getReallighttype()) && ModelConstant.TRA_LIGHTTYPE == pointLight.getReallighttype()) {
            areaPointService.updatePower(ModelConstant.REAL_LIGHTTYPE, pointLight.getPointid(), 0 - pointLight.getTranum(), pointLight.getTralightid());
        } else {
            areaPointService.updatePower(ModelConstant.REAL_LIGHTTYPE, pointLight.getPointid(), 0 - pointLight.getLednum(), pointLight.getLedlightid());
        }
        return pointLightDao.deleteById(String.valueOf(id));
    }

    @Override
    public int update(PointLight pointLight) {
        PointLight lastPointLight = pointLightDao.getById(String.valueOf(pointLight.getId()));
        areaPointService.updatePower(ModelConstant.TRA_LIGHTTYPE, lastPointLight.getPointid(), 0 - lastPointLight.getTranum(), lastPointLight.getTralightid());
        areaPointService.updatePower(ModelConstant.LED_LIGHTTYPE, lastPointLight.getPointid(), 0 - lastPointLight.getLednum(), lastPointLight.getLedlightid());
        if (IntegerUtils.isEmptyAndNull(lastPointLight.getReallighttype()) && ModelConstant.TRA_LIGHTTYPE == lastPointLight.getReallighttype()) {
            areaPointService.updatePower(ModelConstant.REAL_LIGHTTYPE, lastPointLight.getPointid(), 0 - lastPointLight.getTranum(), lastPointLight.getTralightid());
        } else {
            areaPointService.updatePower(ModelConstant.REAL_LIGHTTYPE, lastPointLight.getPointid(), 0 - lastPointLight.getLednum(), lastPointLight.getLedlightid());
        }
        pointLight.setTralightid(novaDeviceLightDao.getLightByCode(
                pointLight.getTralightcode(),
                ModelConstant.TRA_LIGHTTYPE).getId());
        pointLight.setLedlightid(novaDeviceLightDao.getLightByCode(
                pointLight.getLedlightcode(),
                ModelConstant.LED_LIGHTTYPE).getId());
        areaPointService.updatePower(ModelConstant.TRA_LIGHTTYPE, pointLight.getPointid(), pointLight.getTranum(), pointLight.getTralightid());
        areaPointService.updatePower(ModelConstant.LED_LIGHTTYPE, pointLight.getPointid(), pointLight.getLednum(), pointLight.getLedlightid());
        if (IntegerUtils.isEmptyAndNull(pointLight.getReallighttype()) && ModelConstant.TRA_LIGHTTYPE == pointLight.getReallighttype()) {
            areaPointService.updatePower(ModelConstant.REAL_LIGHTTYPE, pointLight.getPointid(), pointLight.getTranum(), pointLight.getTralightid());
        } else {
            areaPointService.updatePower(ModelConstant.REAL_LIGHTTYPE, pointLight.getPointid(), pointLight.getLednum(), pointLight.getLedlightid());
        }
        return pointLightDao.updateNotNull(pointLight);
    }

    @Override
    public List<PointLight> findAll() {
        return pointLightDao.findAll();
    }

    @Override
    public PageList<PointLight> findPointLightPageList(PointLight pointLight,
                                                       Page page, String sidx, String sord) {
        if (StringUtils.isNotNullOrEmpty(pointLight.getLedlightcode())) {
            pointLight.setLedlightcode(pointLight.getLedlightcode().trim());
        }
        Query<PointLight> query = pointLightDao.query().addOrder(Order.custom(sord, sidx));
        Filter.addFilterCondition(query, pointLight);
        return query.pageList(page);
    }

    @Override
    public PointLight findById(int id) {
        return pointLightDao.getById(String.valueOf(id));
    }

    @Override
    public PageList<PointLight> findPageExecute(PointLight pointLight, Page page, String sidx, String sord) {
        PageList<PointLight> pageList = new PageList<PointLight>();
        SqlBuffer sqlBuffer = new SqlBuffer("SELECT " +
                "  TraLightCode tralightcode, " +
                "  tra_device.LightName tralightname, " +
                "  tra_device.Power trapower, " +
                "  sum(TraNum) tranum, " +
                "  LedLightCode ledlightcode, " +
                "  led_device.LightName ledlightname, " +
                "  led_device.Power ledpower, " +
                "  sum(LedNum) lednum " +
                "FROM t_point_light " +
                "  LEFT JOIN t_device_light tra_device ON TraLightCode = tra_device.LightCode " +
                "  LEFT JOIN t_device_light led_device ON LedLightCode = led_device.LightCode " +
                "WHERE CustomerInfoId = ? " +
                "GROUP BY TraLightID, TraLightCode, LedLightID, LedLightCode ", false);
        int rowCount = JdbcUtils.count(sqlBuffer.getCountSql(), pointLight.getCustomerinfoid()).intValue();
        List<PointLight> pointLights = JdbcUtils.query_limit(
                PointLight.class, sqlBuffer.getSelectSql(), page, pointLight.getCustomerinfoid());
        page.make(rowCount);
        pageList.setDatas(pointLights);
        pageList.setPage(page);
        return pageList;
    }

    @Override
    public List<PointLight> findByExample(PointLight pointLight) {
        List<PointLight> pointLights = pointLightDao.findByExample(pointLight);
        return pointLights;
    }
}
